package com.gasstion.controller;

import com.gasstion.dao.MembersDao;
import com.gasstion.entity.Members;
import com.gasstion.service.MembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;

@Controller
@RequestMapping("/members")
@Scope("prototype")
public class MembersController  {
    @Autowired
    private MembersDao membersDao;
    @Autowired
    MembersService membersService;
    @RequestMapping("/query")
    @ResponseBody
    public List<Members> query(){
        return membersDao.query();
    }


    @RequestMapping("/querySearch")
    @ResponseBody
    public Map<String,Object> querySearch(String vipName, int offset, int limit){
        List<Members> list = membersService.findAll(vipName,offset,limit) ;
        long count = membersService.findTotalCount(vipName);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",list) ;
        map.put("total",count) ;
        return  map ;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> save(Members members, int pn){
        int max=200000;
        int min=100000;
        Random random = new Random();
        int vipIdStr = random.nextInt(max)%(max-min+1) + min;
        String vipId = "GAS"+vipIdStr;
        members.setVipId(vipId);
        membersDao.save(members);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",membersService.findAll("",(pn-1) * 2,2)) ;
        map.put("total",membersService.findTotalCount("")) ;
        return map;
    }


    @RequestMapping("/email")
    @ResponseBody
    public String email(String email,String detail,int id) throws AddressException,MessagingException{
        String result;
        int max=999999;
        int min=100000;
        Random random = new Random();
        int yzm = random.nextInt(max)%(max-min+1) + min;
        // 以下变量为用户根据自己的情况设置
        String smtphost = "smtp.qq.com"; // 发送邮件服务器
        String user = "502621426@qq.com"; // 邮件服务器登录用户名
        String password = "bjxzcdnywavtbhii"; // 邮件服务器登录密码
        String from = "502621426@qq.com"; // 发送人邮件地址
        String to = email; // 接受人邮件地址
        String subject = "加油站挂失信息"; // 邮件标题
        String body = "您的挂失信息："+detail+"            ，验证码："+yzm; // 邮件内容
        String yzmStr=""+yzm;
        membersDao.toupdate(detail,yzmStr,id);

// 以下为发送程序，用户无需改动
        try {
//初始化Properties类对象
            Properties props = new Properties();
//设置mail.smtp.host属性
            props.put("mail.smtp.host", smtphost);
//设置使用验证
            props.put("mail.smtp.auth","true");
// 获取非共享的session对象
            Session ssn= Session.getInstance(props,null);
//创建一个默认的MimeMessage对象。
            MimeMessage message = new MimeMessage(ssn);
//创建InternetAddress对象
            InternetAddress fromAddress = new InternetAddress(from);
//设置From: 头部的header字段
            message.setFrom(fromAddress);
//创建InternetAddress对象
            InternetAddress toAddress = new InternetAddress(to);
//设置 To: 头部的header字段
            message.addRecipient(Message.RecipientType.TO, toAddress);
//设置 Subject: header字段
            message.setSubject(subject);
// 现在设置的实际消息
            message.setText(body);
//定义发送协议
            Transport transport = ssn.getTransport("smtp");
//建立与服务器的链接
            transport.connect(smtphost, user, password);
//发送邮件
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
//transport.send(message);
//关闭邮件传输
            result = "成功";
            transport.close();
        } catch(Exception m) //捕获异常
        {
            result = "cuowu";
            m.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/yz")
    @ResponseBody
    public boolean yz(String yzm,int id){
        boolean flag = false;
        String yzm1 = membersDao.getyzm(id);
        if(yzm!=null&&yzm!=""){
            if(yzm1.equals(yzm)){
                flag = true;
            }
        }
        return flag;
    }

    @RequestMapping("/guashi")
    @ResponseBody
    public Map<String, Object> yz(int id, int pn){
        membersDao.guashi(id);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",membersService.findAll("",(pn-1) * 2,2)) ;
        map.put("total",membersService.findTotalCount("")) ;
        return map;
    }

    @RequestMapping("/chongzhi")
    @ResponseBody
    public Map<String, Object> chongzhi(int id, int pn,double balance){
        membersDao.chongzhi(id,balance);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",membersService.findAll("",(pn-1) * 2,2)) ;
        map.put("total",membersService.findTotalCount("")) ;
        return map;
    }
    @RequestMapping("/email1")
    @ResponseBody
    public String email1(String email,String detail,int id) throws AddressException,MessagingException{
        String result;
        int max=999999;
        int min=100000;
        Random random = new Random();
        int yzm = random.nextInt(max)%(max-min+1) + min;
        // 以下变量为用户根据自己的情况设置
        String smtphost = "smtp.qq.com"; // 发送邮件服务器
        String user = "502621426@qq.com"; // 邮件服务器登录用户名
        String password = "bjxzcdnywavtbhii"; // 邮件服务器登录密码
        String from = "502621426@qq.com"; // 发送人邮件地址
        String to = email; // 接受人邮件地址
        String subject = "加油站挂失信息"; // 邮件标题
        String body = "您的启用信息："+detail+"如果不是本人操作请勿回复  ，验证码："+yzm; // 邮件内容
        String yzmStr=""+yzm;
        membersDao.toupdate(detail,yzmStr,id);

// 以下为发送程序，用户无需改动
        try {
//初始化Properties类对象
            Properties props = new Properties();
//设置mail.smtp.host属性
            props.put("mail.smtp.host", smtphost);
//设置使用验证
            props.put("mail.smtp.auth","true");
// 获取非共享的session对象
            Session ssn= Session.getInstance(props,null);
//创建一个默认的MimeMessage对象。
            MimeMessage message = new MimeMessage(ssn);
//创建InternetAddress对象
            InternetAddress fromAddress = new InternetAddress(from);
//设置From: 头部的header字段
            message.setFrom(fromAddress);
//创建InternetAddress对象
            InternetAddress toAddress = new InternetAddress(to);
//设置 To: 头部的header字段
            message.addRecipient(Message.RecipientType.TO, toAddress);
//设置 Subject: header字段
            message.setSubject(subject);
// 现在设置的实际消息
            message.setText(body);
//定义发送协议
            Transport transport = ssn.getTransport("smtp");
//建立与服务器的链接
            transport.connect(smtphost, user, password);
//发送邮件
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
//transport.send(message);
//关闭邮件传输
            result = "成功";
            transport.close();
        } catch(Exception m) //捕获异常
        {
            result = "cuowu";
            m.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/doqiyong")
    @ResponseBody
    public Map<String, Object> doqiyong(int id, int pn){
        membersDao.doqiyong(id);
        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",membersService.findAll("",(pn-1) * 2,2)) ;
        map.put("total",membersService.findTotalCount("")) ;
        return map;
    }
    @RequestMapping("/yz1")
    @ResponseBody
    public boolean yz1(String yzm,int id){
        boolean flag = false;
        String yzm1 = membersDao.getyzm(id);
        if(yzm!=null&&yzm!=""){
            if(yzm1.equals(yzm)){
                flag = true;
            }
        }
        return flag;
    }
}
